package com.tan.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tan.entity.PageResult;
import com.tan.entity.Result;
import com.tan.pojo.Brand;
import com.tan.service.IBrandService;
import com.tan.vo.BrandQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tanMax
 * @since 2022-09-30
 */
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController {

    @Autowired
    private IBrandService brandService;

    @PostMapping("/search/{currentPage}/{pageSize}")
    public Result search(@RequestBody BrandQueryVo vo, @PathVariable Integer currentPage, @PathVariable Integer pageSize){
        PageResult data = brandService.search(currentPage,pageSize,vo);
        return toDataResult(data);
    }
    @PostMapping("/add")
    public Result add(@RequestBody Brand brand){
        boolean save = brandService.save(brand);
        return toResult(save);
    }
    @GetMapping("/get")
    public Result selectBrandParent(){
        List list = brandService.selectByParentId();
        return toDataResult(list);
    }
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable String id){
        boolean b = brandService.removeById(id);
        return toResult(b);
    }
    @PutMapping
    public Result updateById(@RequestBody Brand brand){
        boolean b = brandService.updateById(brand);
        return toResult(b);
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable String id){
        Brand byId = brandService.getById(id);
        return toDataResult(byId);
    }
    @GetMapping("/getTwo/{id}")
    public Result getTwo(@PathVariable String id){
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<Brand> list = brandService.list(wrapper);
        return toDataResult(list);
    }
}
